<?php
class TreatmentsController extends AppController {

	var $name = 'Treatments';

	function admin_index() {
		$this->Treatment->recursive = 0;
		$cat_id = !empty($this->params['form']['filter_cat']) ? $this->params['form']['filter_cat'] : 0;
		if($cat_id)
		{
			$this->paginate = array(
				'conditions' => array('treatment_category_id' => $cat_id)
			);
		}
		$this->set('treatments', $this->paginate());
		$this->set('treatment_categories', $this->Treatment->TreatmentCategory->find('all'));
		$this->set('cat_id', $cat_id);
	}

	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid treatment', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('treatment', $this->Treatment->read(null, $id));
	}

	function admin_add() {
		if (!empty($this->data)) {
			$this->Treatment->create();
			if ($this->Treatment->save($this->data)) {
				$this->Session->setFlash(__('The treatment has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The treatment could not be saved. Please, try again.', true));
			}
		}
		$treatmentCategories = $this->Treatment->TreatmentCategory->find('list');
		$this->set(compact('treatmentCategories'));
	}

	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid treatment', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Treatment->save($this->data)) {
				$this->Session->setFlash(__('The treatment has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The treatment could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Treatment->read(null, $id);
		}
		$treatmentCategories = $this->Treatment->TreatmentCategory->find('list');
		$this->set(compact('treatmentCategories'));
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for treatment', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Treatment->delete($id)) {
			$this->Session->setFlash(__('Treatment deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Treatment was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
